package lps;
import static lps.ConnectDB.cn;
import java.sql.*;
/**
 *
 * @author Andy
 */
public class ConnectDB {
    public static Connection cn = null;
    private String portNumber;
    private String username;
    private String pass;
    private String database;
    private String url;
    private String server;
    
    public ConnectDB(){
        portNumber = "1433";
        username ="sa";
        pass = "08091989";
        database = "Book";
        server = "localhost";
        url = "jdbc:sqlserver://"+server+":"+portNumber+";database="+database;
    }

    public ConnectDB(String portNumber, String username, String pass, String database, String server) {
        this.portNumber = portNumber;
        this.username = username;
        this.pass = pass;
        this.database = database;
        this.url = "jdbc:sqlserver://"+this.server+":"+this.portNumber+";database="+this.database;
        this.server = server;
    }
    
    public boolean openConnect(){
        if(cn==null)
         {
             try{
                 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                 cn = DriverManager.getConnection(url,username,pass);
                 if(cn != null)
                     return true;
                 
             }catch(Exception ex){
                 ex.printStackTrace();
                 return false;
             }                 
         }
         return false;
    }


    public boolean closeConnect(){
        if(cn != null){
            try{
                cn = null;
                cn.close();
                return true;
            }catch(Exception ex){
                ex.printStackTrace();
                return false;
            }
        }
        return false;
    }
}
